package com.huitian.net.utils


import com.huitian.net.MysqlConfiguration
import org.apache.spark.SparkContext
import org.apache.spark.broadcast.Broadcast

/**
 * 广播变量 Wrapper
 */
object BroadcastWrapper {
  @volatile var broadcast_instance: Broadcast[Map[String, String]] = _

  @volatile var caroil_instance: Broadcast[Map[String, String]] = _

  @volatile var tos_instance: Broadcast[Map[String, String]] = _
  /**
   * 更新 instance
   * @param sc spark context
   * @param blocking unpersist by default
   */
  def update(sc: SparkContext, blocking: Boolean = false): Unit = {
    if (broadcast_instance != null){
      broadcast_instance.unpersist(blocking)
      broadcast_instance = sc.broadcast(new JdbcUtil( new MysqlConfiguration ).getBroadcast)
    }
  }

  /**
   * 初始化 instance
   * @param sc spark context
   * @return
   */
  def getInstance(sc: SparkContext): Broadcast[Map[String,String]] = {
    if (broadcast_instance == null) {
      synchronized {
        if (broadcast_instance == null) {
          broadcast_instance = sc.broadcast(new JdbcUtil( new MysqlConfiguration ).getBroadcast)
        }
      }
    }
    broadcast_instance
  }

  def getOil(sc: SparkContext): Broadcast[Map[String,String]] = {
    if (caroil_instance == null) {
      synchronized {
        if (caroil_instance == null) {
          caroil_instance = sc.broadcast(new JdbcUtil( new MysqlConfiguration ).getCaroil)
        }
      }
    }
    broadcast_instance
  }


}
